// pages/creat-order/index.js

var zUrl = getApp().globalData.zUrl;
Page({

  /**
   * 页面的初始数据
   */

  data: {
    esc: '',
    token: '',
    tan: 2,
    is_esc: true,
    rideoImg: '/images/ground.png',
    rideoImgC: '/images/ground-gray.png',
    red: 'red',
    gray: 'gray',
    creatType: 0,
    adviser: ''
  },
  tanchuang: function (e) {
   
      this.setData({
        tan: 1
      })
   

  },
  close: function (e) {
    this.setData({
      tan: 2
    })
  },
  creatTel: function (e) {
    var that = this

    that.setData({
      creatTel: e.detail.value,
      creatTelFinsh: e.detail.cursor
    })
    if (e.detail.cursor == '11') {

      //console.log(that.data.token)
     
    }


  },
  creatName: function (e) {

    this.setData({
      creatName: e.detail.value,
    })
  },
  creatHouse: function (e) {
    this.setData({
      creatHouse: e.detail.value,
    })
  },
  creatArea: function (e) {
    this.setData({
      creatArea: e.detail.value,
    })
  },
  creatPrice: function (e) {
    this.setData({
      creatPrice: e.detail.value,
    })
  },
  creatSales: function (e) {
    this.setData({
      creatSales: e.detail.value,
    })
  },
  creatNote: function (e) {
    this.setData({
      creatNote: e.detail.value,
    })
  },
  noSales: function () {
    this.setData({
      rideoImg: '/images/ground.png',
      rideoImgC: '/images/ground-gray.png',
      red: 'red',
      gray: 'gray',
      creatType: 0,
    })
  },
  hasSales() {
    this.setData({
      rideoImg: '/images/ground-gray.png',
      rideoImgC: '/images/ground.png',
      gray: 'red',
      red: 'gray',
      creatType: 1,
    })
  },
  edit() {
    if (this.data.creatHouse){
      this.data.order_info.res.sub_house_type = this.data.creatHouse
    }
    if (this.data.creatArea) {
      this.data.order_info.res.sub_house_type = this.data. creatArea
    }
    if (this.data.creatPrice) {
      this.data.order_info.res.sub_house_type = this.data.creatPrice
    }
    if (this.data.creatHouse) {
      this.data.order_info.res.sub_house_type = this.data.creatHouse
    }
      wx.request({
        url: zUrl+'/api/order/edit',
        data: {
          id:this.data.id,
          sub_tel: this.data.order_info.res.sub_tel,
          sub_name: this.data.order_info.res.sub_name,
          sub_house_type: this.data.order_info.res.sub_house_type,
          sub_house_area: this.data.order_info.res.sub_house_area,
          house_total_price: this.data.order_info.res.house_total_price,
          salesperson: this.data.adviser.account_name,
          u_c_id: this.data.order_info.res.u_c_id,
          clearing_type: this.data.creatType,
          p_id: this.data.order_info.res.p_id,
          note: this.data.order_info.res.note,
        },
        header: {
          'Cookie': 'laravel_session=' + wx.getStorageSync('laravel_session'),
          Accept: 'application/json',
          Authorization: 'Bearer ' + wx.getStorageSync('token')
        },
        method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        //预防乱码的不用管
        success: function (res) {
          wx.hideLoading()
          //console.log(res.data)//处理返回的数据
          if (res.data.code == '200') {
            wx.switchTab({
              url: '/pages/index/index'
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: "none",
              duration: 2000
            })
          }


        },
        fail: function (error) {
          console.log(error)

        },
        complete: function (res) {

        }
      });
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options.id)
    this.setData({
      id: options.id
    })

    this.setData({
      adviser: wx.getStorageSync('adviser')
    }) 
    var that = this;
    wx.request({
      url: zUrl+'/api/order/show',
      method: 'POST',
      header: {
        'Cookie': 'laravel_session=' + wx.getStorageSync('laravel_session'),
        Accept: 'application/json',
        Authorization: 'Bearer ' + wx.getStorageSync('token')
      },
      data: {
        id: options.id
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 200) {
          that.setData({
            order_info: res.data.data
          })
        }
        //console.log(res.data)
      },
      fail: function (error) {

      },
      complete: function (res) {

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})